#include "Symbol.h"
#include "GenerateICode.h"
#include <string.h>

UInt8 symbolData[SYMBOL_DATA_SIZE] = {0};
struct Symbol* symbols;

BOOL DoesIdentifierSymbolExist(char* ascii)
{
	struct Symbol* s;
	for (s = (struct Symbol*)symbolData;
		 s != symbols;
		 ADD_BYTES_TO_P(s, struct Symbol*, sizeof(struct Symbol) + symbolSizes[s->type]))
	{
		if (strcmp(s->name, ascii) == 0)
			return TRUE;
	}

	return FALSE;
}

struct Symbol* FindIdentifierSymbol(char* ascii)
{
	struct Symbol* s;
	for (s = (struct Symbol*)symbolData; s != symbols; ADD_BYTES_TO_P(s, struct Symbol*, sizeof(struct Symbol) + symbolSizes[s->type]))
	{
		if (strcmp(s->name, ascii) == 0)
			return s;
	}

	return NULL;
}

static struct Symbol* AddIdentifier(char* ascii, enum SYMBOL_TYPE type)
{
	struct Symbol* s = symbols;
	symbols->type = type;
	strcpy(symbols->name, ascii);
	++symbols;

	return s;
}

struct Symbol* AddVariableSymbol(char* ascii)
{
	struct Symbol* s = AddIdentifier(ascii, ST_VARIABLE);
	PSUB_SYMBOL(s, struct VariableSymbol)->variable = NULL;
	ADD_BYTES_TO_P(symbols, struct Symbol*, sizeof(struct VariableSymbol));

	return s;
}
